package com.loe.camera;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.fragment.app.FragmentActivity;

/**
 * 权限提示弹框
 *
 * @author zls
 * @since 2023/10/13-11:00
 */
public class PermissionTipDialog
{
    private AlertDialog dialog;

    private boolean isInit = false;

    private ViewGroup rootView;

    private FragmentActivity activity;

    private LoeCamera.Callback okCallback;

    private LoeCamera.Callback cancelCallback;

    private String tip = "";

    public PermissionTipDialog(FragmentActivity activity)
    {
        this.dialog = new AlertDialog.Builder(activity).create();

        this.activity = activity;
    }

    public PermissionTipDialog(FragmentActivity activity, LoeCamera.Callback callback)
    {
        this.dialog = new AlertDialog.Builder(activity).create();

        this.activity = activity;
        this.okCallback = callback;
    }

    public PermissionTipDialog okCallback(LoeCamera.Callback callback)
    {
        this.okCallback = callback;
        return this;
    }

    public PermissionTipDialog cancelCallback(LoeCamera.Callback callback)
    {
        this.cancelCallback = callback;
        return this;
    }

    public PermissionTipDialog tip(String text)
    {
        this.tip = text;
        return this;
    }

    public void show()
    {
        if (!dialog.isShowing())
        {
            dialog.show();
            Window window = dialog.getWindow();
            if(!isInit && window != null)
            {
                dialog.setContentView(R.layout.camera_permission_tip_dialog);
                window.setBackgroundDrawableResource(android.R.color.transparent);
                window.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
                window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                window.setGravity(Gravity.CENTER);
                rootView = (ViewGroup) window.getDecorView();
                rootView.setPadding(0, 0, 0, 0);
                isInit = true;
            }
            onShow();
        }
    }

    private void onShow()
    {
        TextView textTip = rootView.findViewById(R.id.textTip);
        if(tip != null)
        {
            textTip.setText(tip);
        }

        rootView.findViewById(R.id.buttonAgree).setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                if(okCallback != null) okCallback.callback();
                dialog.dismiss();
            }
        });

        rootView.findViewById(R.id.buttonCancel).setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                if(cancelCallback != null) cancelCallback.callback();
                dialog.dismiss();
            }
        });

        dialog.setOnCancelListener(new DialogInterface.OnCancelListener()
        {
            @Override
            public void onCancel(DialogInterface dialogInterface)
            {
                if(cancelCallback != null) cancelCallback.callback();
                dialog.dismiss();
            }
        });
    }
}
